package com.github.nuggets.util;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.nuggets.model.pojo.LoginAccountInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/18 2:10 下午
 */
@Slf4j
public final class LoginAccountUtils {

    private static final String JWT_DETAILS_KEY = "decodedDetails";

    /**
     * 获取登录用户
     * @param auth
     * @return
     */
    public static final LoginAccountInfo getLoginAccount(OAuth2Authentication auth){
        String authStr = JSONUtil.toJsonStr(auth.getDetails());
        JSONObject authJson = JSON.parseObject(authStr);
        return JSON.parseObject(authJson.getString(JWT_DETAILS_KEY), LoginAccountInfo.class);
    }
}
